package cn.edu.cimv.controller;

import cn.edu.cimv.constant.InfoConstant;
import cn.edu.cimv.dao.entity.Recruit;
import cn.edu.cimv.pojo.Message;
import cn.edu.cimv.service.RecruitService;
import cn.edu.cimv.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * Created by 门铎 on 2016/8/2.
 */
@Controller
@RequestMapping("/cimv/recruit")
public class RecruitController {
    @Autowired
    private RecruitService recruitService;
    @Autowired
    private Recruit recruit;
    @Autowired
    private Message message;
    @RequestMapping("list")
    public String list(Model model){
        model.addAttribute("recruitList",recruitService.queryRecruit());
        return "recruit/list";
    }
    @RequestMapping("new")
    public String recruitNew(){
        return "recruit/new";
    }

    @RequestMapping( value = "create", method = RequestMethod.POST)
    public @ResponseBody Message create(@RequestParam("editor") String editor, @RequestParam("title") String title) {
        recruit.setDate(DateUtil.toDateString(new Date()));
        recruit.setContent(editor);
        recruit.setTitle(title);
        recruitService.editRecruit(recruit);
        message.setMsg(InfoConstant.CREATE_RECRUIT_SUCCESS);
        return message;
    }
    @RequestMapping(value = "delete/{id}",method = RequestMethod.GET)
    public @ResponseBody Message delete(@PathVariable("id")int id){
        recruitService.deleteRecruit(id);
        message.setMsg(InfoConstant.DELETE_RECRUIT_SUCCESS);
        return message;
    }
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String updatePage(@PathVariable("id")int id,Model model){
        model.addAttribute("recruit",recruitService.queryRecruitById(id));
        return "recruit/update";
    }
    @RequestMapping(value = "updateRecruit/{id}",method = RequestMethod.POST)
    public @ResponseBody Message updateNews(@PathVariable("id")int id,
                                            @RequestParam("title")String title,
                                            @RequestParam("editor") String editor){
        //修改的内容
        recruit.setId(id);
        recruit.setDate(DateUtil.toDateString(new Date()));
        recruit.setContent(editor);
        recruit.setTitle(title);
        //更新
        recruitService.updateRecruit(recruit);

        message.setMsg(InfoConstant.UPDATE_RECRUIT_SUCCESS);
        return message;

    }
}
